﻿using System;
using System.Diagnostics;
using Map_Creator.RedoUndo.Actions;
using Microsoft.Xna.Framework;
using System.Reflection;
using System.Windows.Forms;
using Map;

namespace Map_Creator.MainForm
{
    partial class Main
    {
        private void NewGrid_ToolStripClick(object sender, EventArgs e)
        {
            var newGrid = new NewGridControl();
            var resault = newGrid.ShowDialog(this);
            if (resault != DialogResult.OK) return;
            var gridSize = new Point(newGrid.X, newGrid.Y);
            ActionHandler.MainActionHandler.AddActionAndExecute(new AddMapAction(gridSize, newGrid.tileSize,
                                                                                 newGrid.ChoosenName,
                                                                                 TreeViewControl.MapTreeView));
            /*
            var map = new MapInfo.Map(gridSize, newGrid.tileSize) { Name = newGrid.ChoosenName };
            var mapNode = new ExtendedTreeNode(newGrid.ChoosenName,NodeType.Map,map);
            map.Node = mapNode;

            //mapNode.ContextMenuStrip = MapMenuStrip;
            mapNode.ImageIndex = 0;

            TreeViewControl.MapTreeView.Nodes.Add(mapNode);

            MapInfo.Map.SelectedMap = map;
            */
        }

        private void NewLayer_ToolStripClick(object sender, EventArgs e)
        {
            if (MapInfo.Map.SelectedMap == null) return;

            ActionHandler.MainActionHandler.AddActionAndExecute(new AddLayerAction(MapInfo.Map.SelectedMap));
            //MapInfo.Map.SelectedMap.AddTopLayer();

            //MapPanel.Invalidate();
        }

        private void LoadTileSet_ToolStripClick(object sender, EventArgs e)
        {
            var selectImage = new OpenFileDialog();
            var retVal = selectImage.ShowDialog(this);
            if (retVal == DialogResult.OK)
            {
                try
                {
                    ActionHandler.MainActionHandler.AddActionAndExecute(new AddTileSetAction(MapInfo.Map.SelectedMap,selectImage.FileName));
                    //MapInfo.Map.SelectedMap.AddTileSet(selectImage.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Bad TileSet Image");
                    if (Debugger.IsAttached)
                        MessageBox.Show(ex.Message);
                }
            }
        }

        private void gridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var grip = sender as ToolStripMenuItem;
            MapPanel.IsDrawGrid = grip.Checked;
            MapPanel.Invalidate();
        }

        private void resetZoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MapInfo.Map.SelectedMap.ChangeZoom(MapInfo.Map.SelectedMap.TileSize - MapInfo.Map.SelectedMap.ZoomedTileSize);
        }

        private void costumSaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var selectDll = new OpenFileDialog();
            var retVal = selectDll.ShowDialog(this);
            if (retVal == DialogResult.OK)
            {
                try
                {
                    var assembly = Assembly.LoadFile(selectDll.FileName);

                    var types = assembly.GetTypes();
                    foreach (var type in types)
                    {
                        var method = type.GetMethod("Save",new[]{typeof(MapStruct)});

                        if (method == null) continue;

                        method.Invoke(null, new object[] {MapInfo.Map.SelectedMap});
                        return;
                    }
                    
                }
                catch (Exception)
                {
                    MessageBox.Show("Bad Costum Save Dll");
                }
            }
        }

        private void newDataGridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MapInfo.Map.SelectedMap == null) return;

            var newDataGrid = new NewDataGrid();
            var resault = newDataGrid.ShowDialog(this);
            if (resault != DialogResult.OK) return;

            MapInfo.Map.SelectedMap.AddDataGrid(newDataGrid.ChoosenType,newDataGrid.ChoosenName);
        }

        private void newDataGridOnTileSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MapInfo.Map.SelectedMap == null) return;
            if (MapInfo.Map.SelectedMap.CurrentTileSet == null) return;

            var newDataGrid = new NewDataGrid();
            var resault = newDataGrid.ShowDialog(this);
            if (resault != DialogResult.OK) return;

            MapInfo.Map.SelectedMap.CurrentTileSet.AddDataGrid(newDataGrid.ChoosenType, newDataGrid.ChoosenName);
        }

        private void newObjectLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MapInfo.Map.SelectedMap == null) return;

            MapInfo.Map.SelectedMap.AddObjectLayer();
        }
    }
}